//Override this class if you have another set of custom, generic uis.
package com.roguedevelopment.objecthandles.generics
{
	import com.roguedevelopment.objecthandles.Flex4ChildManager;
	import com.roguedevelopment.objecthandles.Flex4HandleFactory;
	import com.roguedevelopment.objecthandles.ObjectHandles;
	import com.roguedevelopment.objecthandles.VisualElementHandle;
	import com.roguedevelopment.objecthandles.constraints.MaintainProportionConstraint;
	import com.roguedevelopment.objecthandles.decorators.AlignmentDecorator;
	import com.roguedevelopment.objecthandles.decorators.DecoratorManager;
	import com.roguedevelopment.objecthandles.generics.datamodels.OhFlex4DataModel;
	import com.roguedevelopment.objecthandles.generics.datamodels.OhFlex4DataModelText;
	import com.roguedevelopment.objecthandles.generics.uis.*;
	//
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	//
	import mx.core.FlexLoader;
	import mx.core.IVisualElementContainer;
	public class OhFlex4Manager
	{
		protected var transformTool:ObjectHandles;
		//
		public var drawLayer:Sprite;
		public var uiContainer:IVisualElementContainer;
		public function OhFlex4Manager(elementsContainer:IVisualElementContainer, 
										drawLayer:Sprite)
		{
			this.uiContainer = elementsContainer;
			this.drawLayer = drawLayer;
			this.transformTool = new ObjectHandles(Sprite(this.uiContainer), 
													null, 
													new Flex4HandleFactory() , 
													new Flex4ChildManager());
			var d:DecoratorManager = new DecoratorManager(this.transformTool, 
															this.drawLayer);
			d.addDecorator(new AlignmentDecorator());
		}
		public function createShape(info:Rectangle):OhFlex4Shape
		{
			var m:OhFlex4DataModel = new OhFlex4DataModel();
			m.x = info.x;
			m.y = info.y;
			m.width = info.width;
			m.height = info.height;
			var s:OhFlex4Shape = new OhFlex4Shape();
			s.model = m;
			this.transformTool.registerComponent(m, s);
			this.uiContainer.addElement(s);
			return s;
		}
		public function createImage(info:Rectangle, source:String):OhFlex4Image
		{
			var m:OhFlex4DataModel = new OhFlex4DataModel();
			m.x = info.x;
			m.y = info.y;
			m.width = info.width;
			m.height = info.height;
			var s:OhFlex4Image = new OhFlex4Image();
			s.source = source;
			s.model = m;
			this.transformTool.registerComponent(m, 
												s, 
												null, 
												true, 
												new Array(new MaintainProportionConstraint()));
			this.uiContainer.addElement(s);
			return s;
		}
		public function createRichEditableText(info:Rectangle, text:XML):OhFlex4RichEditableText
		{
			var m:OhFlex4DataModelText = new OhFlex4DataModelText();
			m.x = info.x;
			m.y = info.y;
			m.width = info.width;
			m.height = info.height;
			m.text = text;
			var t:OhFlex4RichEditableText = new OhFlex4RichEditableText();
			t.model = m;
			this.transformTool.registerComponent(m, t);
			this.uiContainer.addElement(t);
			return t;
		}
		public function onSurfaceClick(event:MouseEvent):void
		{
			if(event.target is FlexLoader) return;
			if(event.target is OhFlex4Shape) return;
			if(event.target is VisualElementHandle) return;
			if(event.target is OhFlex4RichEditableText) return;
			this.transformTool.selectionManager.clearSelection();
		}
	}
}